package cn.itcast.bos.Service.Impl;

import cn.itcast.bos.Dao.CustomerRepository;
import cn.itcast.bos.Service.CustomerService;
import cn.itcast.crm.domain.Customer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class   CustomerServiceImpl implements CustomerService {
    //注入dao
    @Autowired
    private CustomerRepository customerRepository;
    @Override
    //查询没有关联定区的方法
    public List<Customer> findNoAssociationCustomers() {
        //fixedAreaId is null;
        return customerRepository.findByFixedAreaIdIsNull();
    }

    @Override
    //查询与定区关联的方法
    public List<Customer> findHasAssociationFixedAreaCustomers(String fixedAreaId) {
        //...where fixedAreaId=?
        return customerRepository.findByFixedAreaId(fixedAreaId);
    }

    @Override
    //修改关联定区的方法
    public void associationCustomersToFixedArea(String customerId, String fixedAreaId) {
        //先判断customerId是否为空
        if(StringUtils.isBlank(customerId)){
            return;
        }
        //先将所有的与该定区关联的客户的定区Id设为null
        customerRepository.clearFixedAreaId(fixedAreaId);
        //截取customerId
        System.out.println(customerId+"-----------"+fixedAreaId);
        String[] customerArray = customerId.split(",");
        for(String ids :customerArray){
            Integer id = Integer.parseInt(ids);
            customerRepository.updateFixedareaById(fixedAreaId,id);
        }
    }

    @Override
    public void save(Customer c) {
        customerRepository.save(c);
    }

    @Override
    //注册的方法
    public void registCustomer(Customer customer) {
        customerRepository.save(customer);
    }

    @Override
    //查找是否绑定邮箱的方法
    public Customer findByTelephone(String telephone) {
        return customerRepository.findByTelephone(telephone);
    }

    @Override
    //修改状态
    public void UpdateTypeByTelephone(String telephone) {
        customerRepository.UpdateTypeByTelephone(telephone);
    }



    @Override
    public Customer loginbyUsername(String username, String password) {
        return customerRepository.findByUsernameAndPassword(username,password);
    }

    @Override
    public String fixidAreaID(String address) {
        return customerRepository.findByFixedAreaIdByAddress(address);
    }


}
